package com.ktwlsoft.fundmanage.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.fundmanage.domain.entity.BankInfo;
import com.ktwlsoft.fundmanage.domain.repository.BankInfoRepository;
import com.ktwlsoft.fundmanage.mapper.CurrencyMapper;
import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.ResultType;
import com.ktwlsoft.fundmanage.utils.KeyWorkerUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 银行账号配置
 *
 * @author Name on 2019/9/6 12:04
 * @version 1.0
 */
@Service
public class BankInfoService {

    /**银行账号配置*/
    private final BankInfoRepository bankInfoRepository;

    /**通用*/
    private final CurrencyMapper currencyMapper;

    public BankInfoService(BankInfoRepository bankInfoRepository, CurrencyMapper currencyMapper){
        this.bankInfoRepository = bankInfoRepository;
        this.currencyMapper = currencyMapper;
    }

    /**
      * 分页 on 2019/9/6 12:21
      *
      * @param keyWords keyWords
      * @param businessType 业务类型
      * @param bankType 银行类型
      * @return BaseResult<?>
      */
    public BaseResult<?> findPage(int pn,int pageSize,String keyWords,BankInfo.BusinessType businessType,BankInfo.BankType bankType,String type){
        JSONObject jsonObject = new JSONObject();
        if(keyWords != null && !"".equals(keyWords.trim())){
            jsonObject.put("keyWords",keyWords);
        }
        if(businessType != null){
            jsonObject.put("businessType",businessType.toString());
        }
        if(bankType != null){
            jsonObject.put("bankType",bankType.toString());
        }
        jsonObject.put("type",type);
        PageHelper.startPage(pn,pageSize);
        return BaseResult.body(new PageInfo<>(currencyMapper.bankInfoList(jsonObject)), ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 新增或修改 on 2019/9/6 12:29
      *
      * @param bankInfo bankInfo
      * @return BaseResult<?>
      */
    public BaseResult<?> save(BankInfo bankInfo){
        if(bankInfo.getBusinessType() == null){
            return BaseResult.body(bankInfo,ResultType.BUSINESS_ERROR).withMessage("业务类型不能为空");
        }
        if(bankInfo.getBankType() == null){
            return BaseResult.body(bankInfo,ResultType.BUSINESS_ERROR).withMessage("银行类型不能为空");
        }
        if(bankInfo.getId() == null || bankInfo.getId() <= 0){
            bankInfo.setId(KeyWorkerUtil.nextId());
            bankInfo.setCreatedAt(new Date());
            bankInfo.setActive(true);
        }
        if(!bankInfo.getBankType().equals(BankInfo.BankType.BANK) && bankInfo.getBankId() != null){
            BankInfo info = bankInfoRepository.findById(bankInfo.getBankId()).orElse(null);
            if(info == null){
                return BaseResult.body(bankInfo,ResultType.BUSINESS_ERROR).withMessage("该银行不存在");
            }
            bankInfo.setBankName(info.getBankName());
            bankInfo.setBankCode(info.getBankCode());
        }
        bankInfo.setUpdatedAt(new Date());
        try {
            bankInfoRepository.save(bankInfo);
            if(!bankInfo.getBankType().equals(BankInfo.BankType.NUMBER)) {
                List<BankInfo> bankInfos = bankInfoRepository.findBybankId(bankInfo.getId());
                bankInfos.forEach(value ->{
                    value.setBankName(bankInfo.getBankName());
                    value.setBankCode(bankInfo.getBankCode());
                    value.setBankId(bankInfo.getId());
                });
                bankInfoRepository.saveAll(bankInfos);
            }
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(bankInfo,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(bankInfo,ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 详情 on 2019/9/6 14:29
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> getDetails(Long id){
        return BaseResult.body(bankInfoRepository.findById(id).orElse(new BankInfo()),ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 修改是否启用状态 on 2019/9/6 14:39
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> updateActive(Long id){
        BankInfo bankInfo = bankInfoRepository.findById(id).orElse(null);
        if(bankInfo == null){
            return BaseResult.body(id,ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        if(bankInfo.isActive()){
            bankInfo.setActive(false);
        }else{
            bankInfo.setActive(true);
        }
        try {
            bankInfoRepository.save(bankInfo);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(id,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(id,ResultType.BUSINESS_SUCCESS);
    }

    /**
      * 删除 on 2019/9/6 14:43
      *
      * @param id id
      * @return BaseResult<?>
      */
    public BaseResult<?> delete(Long id){
        BankInfo bankInfo = bankInfoRepository.findById(id).orElse(null);
        if(bankInfo == null){
            return BaseResult.body(id,ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        try {
            bankInfoRepository.delete(bankInfo);
        }catch (Exception e){
            e.printStackTrace();
            return BaseResult.body(id,ResultType.BUSINESS_ERROR);
        }
        return BaseResult.body(id,ResultType.BUSINESS_SUCCESS);
    }
}